home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13838 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  67 lines

  1. Path: newsflash.concordia.ca!not-for-mail
  2. From: Didier Guillevic <didier@cenparmi.concordia.ca>
  3. Newsgroups: comp.lang.c++
  4. Subject: Template Class Static Members Initialization
  5. Date: Wed, 27 Mar 1996 11:34:08 -0500
  6. Organization: CENPARMI (Centre for Pattern Recognition and Machine Intelligence)
  7. Message-ID: <31596E00.41C67EA6@cenparmi.concordia.ca>
  8. NNTP-Posting-Host: jofa.cenparmi.concordia.ca
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
  13. CC: didier
  14.  
  15. Hi,
  16.  
  17. Having troubles with the initialization of static members of a template class.
  18. (C++ Primer, S.B. Lippman, 2nd edition, reprint June 93, Section 7.5, pp.369-371)
  19. Any idea if that's due to my compiler or ?!  Would that compile on yours?
  20. Here is a simple example that will not compile with g++:
  21.  
  22. ------
  23. #include <iostream.h>
  24.  
  25. template <class T>
  26. class Test
  27. {
  28.   private:
  29.     T n;  
  30.   public:
  31.     Test( T i ) : n(i) { count++; }
  32.     static int count;
  33. };
  34.  
  35. template <class T>
  36.   int Test<T>::count = 0;
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.   Test<int>    t1(1);
  41.   Test<double> t2(2.0);
  42.   
  43.   cout << Test<int>::count << endl;
  44. }
  45. ------
  46.  
  47. The compiler complains with the following:
  48.  
  49.   test.cc:14: sorry, not implemented: static data member templates
  50.   test.cc:14: end of file read inside definition
  51.  
  52. The non-template version of class Test compiles just fine.
  53. The compiler seems to complain that the class Test<int> and
  54. Test<double> have not yet been instantiated.  But this is the
  55. syntax adopted in Lippman's book.
  56.  
  57. Thanks for any help.
  58.  
  59. Didier
  60. --------------------------------------------------------------
  61. Didier Guillevic
  62. CENPARMI, Suite GM-606, Concordia University
  63. 1455 de Maisonneuve West, Montreal, QC, H3G 1M8, Canada
  64. tel: 1 (514) 848-2843,  fax: 1 (514) 848-4522
  65. url: http://www.cenparmi.concordia.ca/~people/didier/
  66. --------------------------------------------------------------
  67.